<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>{$game.name}</title>
    <link rel="stylesheet" href="__CSS__/index.css">
    <link rel="stylesheet" href="__JS__/layui/css/layui.css" media="all">
</head>
<script>
    // 动态加载js文件
    function loadScript(url, callback) {
        const script = document.createElement('script');
        script.src = url;
        script.type = 'text/javascript';

        script.onload = function() {
            if (callback) callback();
        };

        script.onerror = function() {
            console.error('脚本加载失败: ' + url);
            if (callback) callback(new Error('加载失败'));
        };

        document.head.appendChild(script);
    }
</script>
<body>
    <div class="bg-container" {if condition="!empty($game['Bgimg'])"}style="background: url('{$game.Bgimg}') no-repeat center;"{/if}></div>
    <div id="game-container" {if condition="!empty($game['Bgimg'])"}style="margin-top: 8%;height: 96.5%;" {/if}>
        <div class="loading">游戏加载中...</div>
        <iframe id="game-frame" src="{$game.api_login_url}"></iframe>
    </div>

    <div class="float-ball" id="floatBall" style="display: none;">
        <span></span>
    </div>
    <script src="__JS__/jquery.min.js"></script>
    <script src="__JS__/layui/layui.all.js"></script>
    <script src="__JS__/faxing/commom.js"></script>
    <script>
        //url初始化
        const loginUrl = "{:url('/aiqiH5/login')}";//注册登录
        const createRoleUrl = "{:url('/game/createRole')}";//创建角色
        const FromUrl = "{:url('/game/index')}";//登录弹窗
        const OrderYayUrl = "{:url('/aiqiH5/order')}";//下单支付
        const feihuo_creteroleUlr = "{:url('/aiqiH5/create_role')}";//创建角色

        const gid = "{$gid}";
        const cid = "{$cid}";
        const token = localStorage.getItem("token");
        const game_url = localStorage.getItem("game_url");
        const extdata = "{$extdata}";
        let open_game = 0;

        //获取本地缓存token
        const gameFrame = document.getElementById('game-frame');
        const loading = document.querySelector('.loading');
        console.log("渠道id："+cid);
        console.log("token："+token);
        if(token){
            gameFrame.style.display = 'block';
            loading.style.display = 'none';
            gameFrame.src = game_url;
        }
    </script>

    {if condition="$cid == 7"}
    <script src="__JS__/faxing/liebao.js"></script>
    {/if}
    {if condition="$cid == 8"}
    <script src="__JS__/faxing/YY.js"></script>
    {/if}
    {if condition="$cid == 9"}
    <script type="text/javascript" src="//pulsdk.7724.com/channelsdk/jquery.js"></script>
    <script src="__JS__/faxing/qiqi.js"></script>
    {/if}
    {if condition="$cid == 10"}
    <script src="__JS__/faxing/feihuo.js"></script>
    {/if}
    {if condition="$cid == 11"}
    <script src="__JS__/faxing/qiqisy.js"></script>
    {/if}
    {if condition="$cid == 12"}
    <script src="__JS__/faxing/flash2144.js"></script>
    {/if}
    {if condition="$cid == 13"}
    <script src="__JS__/faxing/duoyou.js"></script>
    {/if}
    {if condition="$cid == 14"}
    <script src="__JS__/faxing/zixia.js"></script>
    {/if}
    {if condition="$cid == 15"}
    <script src="__JS__/faxing/yuewen.js"></script>
    {/if}
    {if condition="$cid == 16"}
    <script src="__JS__/faxing/tongchengyou.js"></script>
    {/if}
    {if condition="$cid == 17"}
    <script src="__JS__/faxing/qiqi23.js"></script>
    {/if}
    {if condition="$cid == 18"}
    <script src="__JS__/faxing/souyouji.js"></script>
    {/if}
    {if condition="$cid == 19"}
    <script src="__JS__/faxing/san60.js"></script>
    {/if}
    {if condition="$cid == 20"}
    <script src="__JS__/faxing/hupu.js"></script>
    {/if}
    {if condition="$cid == 22"}
    <script src="__JS__/faxing/zhongyou.js"></script>
    {/if}
    <script>
        // 父窗口代码
        window.addEventListener('message', (event) => {
            // 安全考虑：验证消息来源
            // if (event.origin !== "https://your-iframe-domain.com") return;
            console.log('接收到更新111:', event);

            // 检查消息类型
            if (event.data && event.data.requestType === 'payment') {
                event.data.params.token = token;
                console.log('支付数据:', event.data.params);
                // 发行下单
                getChannelOrder(event.data.params);
            }
            if (event.data && event.data.requestType === 'create_role' && token) {
                event.data.params.token = token;
                event.data.params.cid = cid;
                console.log('创建角色数据:', event.data.params);
                $.post(createRoleUrl,event.data.params,function(info) {
                    console.log("创建角色结果:",info);
                    event.data.params.is_save = 0;
                    if(info.code == 1){
                        if(info.data.is_save == 1){
                            event.data.params.is_save = 1;
                        }
                        faXingCreateRole(event.data.params);
                    }
                });
            }
        });

        function getChannelOrder(data) {
            selectedPaymentMethod = '1';
            // document.getElementById('paymentModal').style.display = 'block';
            data.type = "order";
            data.token = token;
            data.cid = cid;
            $.post(OrderYayUrl,data,function(result) {
                // 下单接口
                console.log(result.data);
                pay(result.data);
            });
        }

        // 发行角色上传
        function faXingCreateRole(data) {
            console.log("发行角色创建：",data);
            if(data.cid == '7'){
                liebao_creterole(data);
            }else if(data.cid == '8'){
                YY_creterole(data);
            }else if(data.cid == '9'){
                qiqi_creterole(data);
            }else if(data.cid == '10'){
                feihuo_creterole(data);
            }else if(data.cid == '11'){
                Qiqisy_creterole(data);
            }else if(data.cid == '12'){
                flash2144_creterole(data);
            }else if(data.cid == '13'){
                duoyou_creterole(data);
            }else if(data.cid == '18'){
                souyouji_creterole(data);
            }
        }

        // 检查登录状态
        function checkLoginStatus() {
            if(token){
                return true;
            }
            return false;
        }

        // 页面加载完成后检查登录状态
        window.onload = function() {
            console.log('页面加载完成');
            if (checkLoginStatus()) {
                // 显示游戏
                gameFrame.style.display = 'block';
                loading.style.display = 'none';
            }

        };
    </script>
</body>
</html>